package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.ExpertProductScoreDO;
import com.example.makerspace.dto.ExpertProductScoreDTO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.ExpertProductScoreMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class ExpertProductScoreDAO extends  BaseDAO<ExpertProductScoreDO> {
    @Resource
    private ExpertProductScoreMapper expertProductScoreMapper;

    @PostConstruct
    public void init(){
        this.mapper = expertProductScoreMapper;
    }

    public IPage<ExpertProductScoreDO> selectByPageAndUserName(Pagination pagination, String username) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<ExpertProductScoreDO> selectByPage(Pagination pagination) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<ExpertProductScoreDO> selectByPageLikeUserName(Pagination pagination, String username, String name) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.like("task_title",name).or().like("outcome_title",name).eq("username",username);
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public List<ExpertProductScoreDO> selectByTaskId(ExpertProductScoreDTO expertProductScoreDTO) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.eq("task_id",expertProductScoreDTO.getTaskId()).eq("outcome_id",expertProductScoreDTO.getOutcomeId()).eq("username",expertProductScoreDTO.getUsername());
        return  mapper.selectList(wrapper);
    }

    public List<ExpertProductScoreDO> selectById(Integer taskId) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.eq("task_id",taskId);
        return  mapper.selectList(wrapper);
    }

    public List<ExpertProductScoreDO>  selectByTaskIdAndOutComeId(Integer taskId, Integer id, String username) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.eq("task_id",taskId).eq("outcome_id",id).eq("username",username);
        return  mapper.selectList(wrapper);
    }

    public List<ExpertProductScoreDO> selectByTaskIdAdnOucComeId(Integer taskId, Integer id) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.eq("task_id",taskId).eq("outcome_id",id);
        return mapper.selectList(wrapper);
    }

    public void deleteByTaskIdAndUserName(Integer taskId, String username) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.eq("task_id",taskId).eq("username",username);
        mapper.delete(wrapper);
    }

    public List<ExpertProductScoreDO> selectByTaskAndUserName(Integer taskId, String username) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.eq("task_id",taskId).eq("username",username);
        return mapper.selectList(wrapper);
    }

    public List<ExpertProductScoreDO> selectByUserName(String name) {
        QueryWrapper<ExpertProductScoreDO> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name).eq("task_id",12);
        return  mapper.selectList(wrapper);
    }
}
